package com.dao;

import com.entity.UpFile;
import com.entity.Users;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public  class UpFileConnection {
    public static Connection getConn() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/manger","root","");
    }

    public static int addSql(String IP, String filename , String filetype , String filesize , String filedate , String download) throws SQLException {
        String sql = "INSERT INTO upfile (IP,filename,filetype,filesize,filedata,download) values (?,?,?,?,?,?) ";
        QueryRunner queryRunner = new QueryRunner();
        return queryRunner.update(getConn(), sql , IP,filename,filetype,filesize,filedate,download);
    }

    public static List<UpFile> selectALl() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from upfile";
        List<Users> query  = null;
        return queryRunner.query(getConn(), sql, new BeanListHandler<>(UpFile.class));
    }
}
